package io.renren.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import io.renren.entity.DataContractpayDetailEntity;
import io.renren.entity.DataContractpayEntity;
import io.renren.entity.HisContractEntity;
import io.renren.entity.HisContractpayDetailEntity;
import io.renren.entity.HisContractpayEntity;
import io.renren.entity.xtd.DataContractEntityXtd;
import io.renren.entity.xtd.DataContractpayDetailEntityXtd;
import io.renren.entity.xtd.DataContractpayEntityXtd;
import io.renren.entity.xtd.HisContractEntityXtd;
import io.renren.entity.xtd.HisContractpayDetailEntityXtd;
import io.renren.entity.xtd.HisContractpayEntityXtd;
import io.renren.service.DataContractpayDetailService;
import io.renren.service.DataContractpayService;
import io.renren.service.HisContractService;
import io.renren.service.HisContractpayDetailService;
import io.renren.service.HisContractpayService;
import io.renren.service.xtd.DataContractServiceXtd;
import io.renren.service.xtd.DataContractpayDetailServiceXtd;
import io.renren.service.xtd.DataContractpayServiceXtd;
import io.renren.service.xtd.HisContractServiceXtd;
import io.renren.service.xtd.HisContractpayDetailServiceXtd;
import io.renren.service.xtd.HisContractpayServiceXtd;
import io.renren.utils.PageUtils;
import io.renren.utils.R;


/**
 * 合同分批表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-05-31 13:49:46
 */
@Controller
@RequestMapping("datacontractpay")
public class DataContractpayController {
	@Autowired
	private DataContractpayDetailServiceXtd dataContractpayDetailServiceXtd;
	@Autowired
	private DataContractServiceXtd dataContractServiceXtd;
	@Autowired
	private HisContractpayDetailServiceXtd hisContractpayDetailServiceXtd;
	@Autowired
	private HisContractServiceXtd hisContractServiceXtd;
	@Autowired
	private HisContractpayServiceXtd hisContractpayServiceXtd;
	@Autowired
	private DataContractpayService dataContractpayService;
	@Autowired
	private DataContractpayServiceXtd dataContractpayServiceXtd;
	
	@RequestMapping("/datacontractpay.html")
	public String list(){
		return "datacontractpay/datacontractpay.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("datacontractpay:list")
	public R list(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<DataContractpayEntity> dataContractpayList = dataContractpayService.queryList(map);
		int total = dataContractpayService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(dataContractpayList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("datacontractpay:info")
	public R info(@PathVariable("id") String id){
		DataContractpayEntity dataContractpay = dataContractpayService.queryObject(id);
		
		return R.ok().put("dataContractpay", dataContractpay);
	}
	
	
	@ResponseBody
	@RequestMapping("/infoXtd/{id}")
	public R infoXtd(@PathVariable("id") String id){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", 0);
		map.put("limit", 1);
		map.put("hisid", id);
		
		HisContractpayEntityXtd hisContractpay = hisContractpayServiceXtd.queryObjectXtd(map);
		if (hisContractpay == null) {
			Map<String, Object> mapb = new HashMap<>();
			mapb.put("offset", 0);
			mapb.put("limit", 1);
			mapb.put("id", id);
			List<DataContractpayEntityXtd> dataContractpayList = dataContractpayServiceXtd.queryListXtd(mapb);
			if (dataContractpayList==null || dataContractpayList.size()==0) {
				return R.ok().put("hisContractpay", "").put("hisContractpayDetailList", "");
			} else {
				DataContractEntityXtd dataContract = dataContractServiceXtd.queryObjectXtd(dataContractpayList.get(0).getContractid());
				if (dataContract==null) {
					return R.ok().put("hisContractpay", "").put("hisContractpayDetailList", "");
				}
				Map<String, Object> mapc = new HashMap<>();
				mapc.put("contractpayid", id);
				List<DataContractpayDetailEntityXtd> dataContractpayDetailList = dataContractpayDetailServiceXtd.queryListXtd(mapc);
				if (dataContractpayDetailList==null || dataContractpayDetailList.size()==0) {
					return R.ok().put("hisContractpay", dataContractpayList.get(0)).put("hisContractpayDetailList", "");
				} else {
					String[] zhuId=new String[dataContractpayDetailList.size()];
	                String[] zhuWorkNo=new String[dataContractpayDetailList.size()];
	                String[] zhuName=new String[dataContractpayDetailList.size()];
	                String[] zhuBi=new String[dataContractpayDetailList.size()];
	                String[] zhuGe=new String[dataContractpayDetailList.size()];
	                zhuId=dataContract.getSplitemployeeidlist().split(",");
	                zhuWorkNo=dataContract.getSplitEmployeeWorkNoList().split(",");
	                zhuName=dataContract.getSplitEmployeeNameList().split(",");
	                zhuBi=dataContract.getSplitpercent().split(",");
	                zhuGe=dataContract.getEveryoneNo().split(",");
	                for (int i = 0; i < dataContractpayDetailList.size(); i++) {
	                	for(int n=0;n<zhuId.length;n++){
	                		if (dataContractpayDetailList.get(i).getEmployeeid().equals(zhuId[n])) {
	                			dataContractpayDetailList.get(i).setSplitEmployeeName(zhuName[n]);
	                			dataContractpayDetailList.get(i).setSplitEmployeeWorkNo(zhuWorkNo[n]);
	                			dataContractpayDetailList.get(i).setSplitpercent(zhuBi[n]);
	                			dataContractpayDetailList.get(i).setEveryoneNo(zhuGe[n]);
	                			break;
							}
	                	}
						
					}
	                return R.ok().put("hisContractpay", dataContractpayList.get(0)).put("hisContractpayDetailList", dataContractpayDetailList);
				}
				
               
			
			}
			
		} else {
			Map<String, Object> mapa = new HashMap<>();
			mapa.put("hisid", hisContractpay.getContractid());
			mapa.put("offset", 0);
			mapa.put("limit", 1);
			List<HisContractEntityXtd> hisContract = hisContractServiceXtd.queryObjectXtd(mapa);
			if (hisContract==null || hisContract.size()==0) {
				return R.ok().put("hisContractpay", "").put("hisContractpayDetailList", "");
			}
			mapa.put("contractpayid", id);
			List<HisContractpayDetailEntityXtd> hisContractpayDetailList = hisContractpayDetailServiceXtd.queryListXtd(mapa);
			if (hisContractpayDetailList==null || hisContractpayDetailList.size()==0) {
				return R.ok().put("hisContractpay", hisContractpay).put("hisContractpayDetailList", "");
			} else {
                String[] zhuId=new String[hisContractpayDetailList.size()];
                String[] zhuWorkNo=new String[hisContractpayDetailList.size()];
                String[] zhuName=new String[hisContractpayDetailList.size()];
                String[] zhuBi=new String[hisContractpayDetailList.size()];
                String[] zhuGe=new String[hisContractpayDetailList.size()];
                zhuId=hisContract.get(0).getSplitemployeeidlist().split(",");
                zhuWorkNo=hisContract.get(0).getSplitEmployeeWorkNoList().split(",");
                zhuName=hisContract.get(0).getSplitEmployeeNameList().split(",");
                zhuBi=hisContract.get(0).getSplitpercent().split(",");
                zhuGe=hisContract.get(0).getEveryoneNo().split(",");
                for (int i = 0; i < hisContractpayDetailList.size(); i++) {
                	for(int n=0;n<zhuId.length;n++){
                		if (hisContractpayDetailList.get(i).getEmployeeid().equals(zhuId[n])) {
                			hisContractpayDetailList.get(i).setSplitEmployeeName(zhuName[n]);
                			hisContractpayDetailList.get(i).setSplitEmployeeWorkNo(zhuWorkNo[n]);
                			hisContractpayDetailList.get(i).setSplitpercent(zhuBi[n]);
                			hisContractpayDetailList.get(i).setEveryoneNo(zhuGe[n]);
                			break;
						}
                	}
					
				}
                return R.ok().put("hisContractpay", hisContractpay).put("hisContractpayDetailList", hisContractpayDetailList);
			}
			
			
		}
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("datacontractpay:save")
	public R save(@RequestBody DataContractpayEntity dataContractpay){
		dataContractpayService.save(dataContractpay);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("datacontractpay:update")
	public R update(@RequestBody DataContractpayEntity dataContractpay){
		dataContractpayService.update(dataContractpay);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("datacontractpay:delete")
	public R delete(@RequestBody String[] ids){
		dataContractpayService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
